package bluejack.game.gal;
import com.eclecticdesignstudio.motion.Actuate;
import hscript.Interp;
import hscript.Parser;
import nme.events.TimerEvent;
import nme.Lib;
import nme.media.Sound;
import nme.media.SoundChannel;
import nme.media.SoundTransform;
import nme.net.URLRequest;
import nme.utils.Timer;

/**
 * ...
 * @author BlueJack
 */

class GALControl 
{
	public var isWait:Bool;
	public var view(getView, null):GALView;
	
	private var bgm:SoundChannel;
	
	private function get_selectIndex():Int 
	{
		return interp.variables.get("selectIndex");
	}
	
	private function set_selectIndex(value:Int):Int 
	{
		interp.variables.set("selectIndex", value);
		return value;
	}
	
	public var selectIndex(get_selectIndex, set_selectIndex):Int;
	private function getView():GALView {
		return facde.view;
	}
	private var facde:GALFacde;
	
	private var data:Array<String>;
	private var labels:Hash<Int>;
	private var step:Int;
	private var parser:Parser;
	private var interp:Interp;
	private var dataLength:Int;
	private var sound:Sound;
	

	public function new(facde:GALFacde) {
		this.facde = facde;
		data = [];
		labels = new Hash();
		step = -1;
		isWait = false;
		parser = new Parser();
		interp = new Interp();
		interp.variables.set("setTextWinStyle", view.setTextWinStyle);
		interp.variables.set("setDelay", view.setDelay);
		interp.variables.set("showText", view.showText);
		interp.variables.set("showName", view.showName);
		interp.variables.set("showSelect", view.showSelect);
		interp.variables.set("wait", wait);
		interp.variables.set("loadBg", view.loadBg);
		interp.variables.set("loadFigure", view.loadFigure);
		interp.variables.set("hideFigure", view.hideFigure);
		interp.variables.set("goto", goto);
		interp.variables.set("playBgm", playBgm);
		interp.variables.set("stopBgm", stopBgm);
	}
	
	public function setData(s:String):Void {
		dataLength = 0;
		var reg:EReg = ~/\n\s*\r/g;
		s = reg.replace(s, "");
		reg = ~/\r?\n/g;
		var lines:Array<String> = reg.split(s);
		reg = ~/(^\s*)|(\s*$)/g;
		var line:String;
		for (i in 0...lines.length) {
			line = reg.replace(lines[i], "");
			if (line.charAt(0)=="*") {
				labels.set(line, dataLength-1);
			}else {
				data.push(line);
				dataLength ++;
			}
		}
		//Lib.trace(labels);
		//for (j in 0...data.length) {
			//Lib.trace(j + " " + data[j]); 
		//}
		view.showTitle();
	}
	
	public function run():Void {
		while (!isWait) {
			step ++;
			if (step >= dataLength) {
				Lib.trace("Game Complete");
				break;
			}
			var data:String = data[step];
			//Lib.trace("step:" + step);
			var program = parser.parseString(data);
			interp.execute(program);
			
		}
	}
	
	public function playBgm(path:String = null):Void {
		if (path != null) {
			if(bgm != null) bgm.stop();
			sound = new Sound(new URLRequest("bgm/" + path));
			bgm = sound.play();
		}else {
			bgm = sound.play(bgm.position);
		}
	}
	
	public function stopBgm(fadeOut:Bool=true):Void {
		if (fadeOut) {
			Actuate.transform(bgm, 2).sound(0);
		}else {
			bgm.stop();
		}
	}
	
	public function goto(label:String):Void {
		step = labels.get(label);
		//run();
	}
	
	public function wait(delay:Float):Void {
		isWait = true;
		Actuate.timer(delay).onComplete(onWaitTimeOut);
	}
	
	private function onWaitTimeOut():Void {
		isWait = false;
		run();
	}
}